package com.example.toolbar;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.toolbar.global.MyApplication;
import com.example.toolbar.net.HttpRequestWrap;
import com.example.toolbar.net.OnResponseHandler;
import com.example.toolbar.net.RequestHandler;
import com.example.toolbar.net.RequestStatus;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class LoginActivity extends Activity {
	private HttpRequestWrap httpRequest;

	@ViewInject(R.id.title)
	private TextView tvTitle;
	SharedPreferences sp;
	private String sessionID;
	private MyApplication application;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main2);
		sp = getSharedPreferences("mysid", 0);
		ViewUtils.inject(this);
		httpRequest = new HttpRequestWrap(this);
		application=(MyApplication) this.getApplication();
	}

	@OnClick({ R.id.btn_send, R.id.btn_sessionID, R.id.btn_login })
	public void onClick(View v) {
		if (v.getId() == R.id.btn_send) {
			Intent intent = new Intent(LoginActivity.this, MainActivity.class);
			startActivity(intent);
		} else if (v.getId() == R.id.btn_sessionID) {
			httpRequest.setMethod(HttpRequestWrap.GET);
			httpRequest.setCallBack(new RequestHandler(this,
					new OnResponseHandler() {
						@Override
						public void onResponse(String result,
								RequestStatus status) {
							if (status == RequestStatus.SUCCESS) {
								JSONObject obj = JSON.parseObject(result);
								if (!obj.getBooleanValue("success")) {
									// ����ʧ��
									Toast.makeText(
											LoginActivity.this,
											obj.getString("message")
													+ sp.getString("session",
															"----"),
											Toast.LENGTH_SHORT).show();

								} else {
									// ����ɹ�
									Toast.makeText(LoginActivity.this, result,
											Toast.LENGTH_SHORT).show();
								}
							}
						}
					}));
			httpRequest
					.send("http://tr.zzapi.gson.cn/api/user/comments?page=1&rows=10");
		} else if (v.getId() == R.id.btn_login) {
			httpRequest.setMethod(HttpRequestWrap.POST);
			httpRequest.setCallBack(new RequestHandler(this,
					new OnResponseHandler() {
						@Override
						public void onResponse(String result,
								RequestStatus status) {
							// ��ȡHttpUtils
							HttpUtils httpUtils = httpRequest.getHttpUtils();
							// ��ȡHttpClient
							DefaultHttpClient httpClient = (DefaultHttpClient) httpUtils
									.getHttpClient();
							// ���Cookie
							CookieStore cs = httpClient.getCookieStore();
							// ���Cookie���������м�ֵ��
							List<Cookie> list = cs.getCookies();
							if (list != null) {
								for (int i = 0; i < list.size(); i++) {
									Cookie c = list.get(i);
									System.out.println("JSESSIONID".equals(c.getName()));
									if ("JSESSIONID".equals(c.getName())) {
										Toast.makeText(LoginActivity.this,
												"登录成功  sid:" + c.getValue(),
												Toast.LENGTH_SHORT).show();
										String sid = c.getValue();
										// ��Session���浽xml�ļ���
										Editor editor = sp.edit();
										editor.putString("session", sid);
										editor.commit();
										sessionID = sid;
										break;
									}
								}
							}
						}
					}));

			// post����
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("userName", "言胜");
			params.put("password", "904042749");
			httpRequest.send("http://tr.zzapi.gson.cn/api/user/login", params);
			application.setHttpRequest(httpRequest);
		}
	}
}
